import '../utils/date_utils_ext.dart';

import '../models/num_range.dart';
import 'form_validator_params.dart';

String? validateDouble(dynamic value, String title, bool isAllowEmpty,
    {NumRange? range, bool mustBePositive = true}) {
  if (!isAllowEmpty) {
    //不允许为空
    if (value == null || value.isEmpty) {
      return "$title不能为空";
    } else {
      return validatePositiveNum(value, mustBePositive, range: range);
    }
  } else {
    //允许为空
    if (value == null || value.isEmpty) {
      return null;
    } else {
      return validatePositiveNum(value, mustBePositive, range: range);
    }
  }
}

String? validatePositiveNum(String value, bool mustBePositive,
    {NumRange? range}) {
  num? numParsed = num.tryParse(value);

  late String error;
  if (mustBePositive == true) {
    error = "须大于0";
  } else {
    error = "须是数字";
  }
  if (numParsed == null) {
    return error;
  } else {
    if (mustBePositive == true && numParsed < 0) {
      return error;
    }
    if (range != null) {
      if (range.isInRange(numParsed) == false) {
        return range.errorInfo;
      }
    }
  }
  return null;
}

String? validateEmpty(dynamic value, String title) {
  if (value == null || value.isEmpty || value.trim().isEmpty) {
    return "不能为空";
  }
  return null;
}

String? validateStringEmpty(String? value, {String? title}) {
  if (value == null || value.isEmpty || value.trim().isEmpty) {
    return "${title ?? ""}不能为空";
  }
  return null;
}

String? validatePositiveInt(String value, bool mustBePositive, NumRange range) {
  num? numParsed = num.tryParse(value);

  if (numParsed == null) {
    return "需正整数";
  } else {
    if (value.contains(".")) {
      return "需正整数";
    } else {
      if (mustBePositive && numParsed < 0) {
        return "需正整数";
      }
    }
    if (range.isInRange(numParsed) == false) {
      return range.errorInfo;
    }
  }
  return null;
}

String? validateInt(
    String? value, String title, bool isAllowEmpty, NumRange range,
    {bool mustBePositive = true, int? length}) {
  if (value == null || value.isEmpty) {
    return isAllowEmpty ? null : "$title不能为空";
  }
  return validatePositiveInt(value, mustBePositive, range);
}

String? validateYear(
    String? value, String title, bool isAllowEmpty, NumRange range) {
  if (value == null || value.isEmpty) {
    return isAllowEmpty ? null : "$title不能为空";
  }
  if (value.length != 4) {
    return "$title必须为四位整数";
  }

  int? year = int.tryParse(value);
  if (year == null || (year.toString().length != value.length)) {
    return "$title格式不正确";
  }

  if (range.isInRange(year) == false) {
    return range.errorInfo;
  }
  return null;
}

String? validateAge(String? value, {NumRange? range}) {
  return validateInt(value, "年龄", false, range ?? ageRange);
}

String? validatePhone(dynamic value, String title, bool isAllowEmpty) {
  final RegExp regExp = RegExp(
      "^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[8-9])|(147,145))\\d{8}\$");
  if (!isAllowEmpty) {
    //不允许为空
    if (value == null || value.isEmpty) {
      return "$title不能为空";
    } else if (!regExp.hasMatch(value)) {
      return "$title格式错误";
    }
  } else {
    //允许为空
    if (value == null || value.isEmpty) {
      return null;
    } else {
      if (!regExp.hasMatch(value)) {
        return "$title格式错误";
      }
    }
  }

  return null;
}

String? validateDate(String? value, String? title) {
  String? result = validateEmpty(value, title ?? "");
  if (result == null) {
    String? standartStr = dateStrToStandardStr(value!);
    if (standartStr == null) {
      return "格式错误，示例:2023-02-11";
    } else {
      DateTime? dt = DateTime.tryParse(standartStr);
      if (dt == null) {
        return "格式错误，示例:2023-02-11";
      } else {
        if (isEqualsWithStandardStr(dt, standartStr) == false) {
          return "日期超限";
        }
      }
    }
  }
  return null;
}

String? validateUserName(String? value, [bool allowEmpty = true]) {
  if (value == null || value.isEmpty == true) {
    if (allowEmpty == false) {
      return "账户名不能为空";
    } else {
      return null;
    }
  }

  if (value.length < 6) {
    return "账户名由大小写字母、下划线或数字组成，且不能小于6位";
  }

  if (value.length > 16) {
    return "账户名由大小写字母、下划线或数字组成，且不能大于16位";
  }

  RegExp reg = RegExp("[a-zA-Z0-9_-]{6,16}");
  List<RegExpMatch> matches = reg.allMatches(value).toList();
  String error = "账户名由大小写字母、下划线或数字组成";
  if (matches.isEmpty == true) {
    return error;
  } else {
    String? matchGroupStr = matches[0].group(0);
    if (matchGroupStr == null || matchGroupStr.length != value.length) {
      return error;
    }
  }
  return null;
}

String? validatePassword(String? value, String title,
    [bool allowEmpty = true]) {
  if (value == null || value.isEmpty == true) {
    if (allowEmpty == false) {
      return "$title不能为空";
    } else {
      return null;
    }
  }

  String commonPrompt = "由大小写字母、数字或特殊字符组成";
  if (allowEmpty == false && value.length < 6) {
    return "$title$commonPrompt，且不能小于6位";
  }

  if (value.length > 16) {
    return "$title$commonPrompt，且不能大于16位";
  }

  RegExp reg = RegExp("(([a-zA-Z0-9])|([!*\$&^%#@]+)){6,16}");
  List<RegExpMatch> matches = reg.allMatches(value).toList();
  String error = "$title$commonPrompt";
  if (matches.isEmpty == true) {
    return error;
  } else {
    String? matchGroupStr = matches[0].group(0);
    if (matchGroupStr == null || matchGroupStr.length != value.length) {
      return error;
    }
  }
  return null;
}
